package com.foreveross.crawl.vo;

import java.io.Serializable;

import org.apache.log4j.Logger;

/** 
 * @ClassName: 任务数据统计 
 * @Description: TODO
 * @author luofangyi
 * @date 2014-7-11 下午6:11:16 
 *  
 */ 
public class TaskStatistics implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4851654913811460769L;
	
	private static Logger logger = Logger.getLogger(TaskStatistics.class);

	/**
	 * 任务总数
	 */
	private int taskTotal = 0;
	
	/**
	 * 执行中的任务数
	 */
	private int taskDoing = 0;
	
	/**
	 * 执行异常任务数
	 */
	private int taskException = 0;
	
	/**
	 * 成功的任务数
	 */
	private int taskSuccess = 0;

	public int getTaskTotal() {
		return taskTotal;
	}

	public void setTaskTotal(int taskTotal) {
		this.taskTotal = taskTotal;
	}

	public int getTaskDoing() {
		return taskDoing;
	}

	public void setTaskDoing(int taskDoing) {
		this.taskDoing = taskDoing;
	}

	public int getTaskException() {
		return taskException;
	}

	public void setTaskException(int taskException) {
		this.taskException = taskException;
	}

	public int getTaskSuccess() {
		return taskSuccess;
	}

	public void setTaskSuccess(int taskSuccess) {
		this.taskSuccess = taskSuccess;
	}

	@Override
	public String toString() {
		return "TaskStatistics [taskTotal=" + taskTotal + ", taskDoing="
				+ taskDoing + ", taskException=" + taskException
				+ ", taskSuccess=" + taskSuccess + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + taskDoing;
		result = prime * result + taskException;
		result = prime * result + taskSuccess;
		result = prime * result + taskTotal;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TaskStatistics other = (TaskStatistics) obj;
		if (taskDoing != other.taskDoing)
			return false;
		if (taskException != other.taskException)
			return false;
		if (taskSuccess != other.taskSuccess)
			return false;
		if (taskTotal != other.taskTotal)
			return false;
		return true;
	}

	public void addTotal(){
		synchronized (TaskStatistics.class) {
			this.setTaskTotal(this.getTaskTotal() + 1);
		}
	}
	
	public void addTotal(int number){
		synchronized (TaskStatistics.class) {
			this.setTaskTotal(this.getTaskTotal() + number);
		}
	}

	public void addDoing(){
		synchronized (TaskStatistics.class) {
			if(this.getTaskTotal() - 1 >= this.getTaskDoing()){
				this.setTaskDoing(getTaskDoing() + 1);
			} else {
				logger.error("addDoing exception");
			}
		}
	}
	
	public void addSuccess(){
		synchronized (TaskStatistics.class) {
			
			if(getTaskDoing() > 0 && (getTaskTotal() - 1) >= getTaskSuccess()){
				this.setTaskSuccess(getTaskSuccess() + 1);
				setTaskDoing(getTaskDoing() - 1);
			} else {
				logger.error("addSuccess exception");
			}
		}
	}
	
	public void addSuccess(int number){
		synchronized (TaskStatistics.class) {
			
			if(getTaskDoing() > 0 && (getTaskTotal() - number) >= getTaskSuccess()){
				this.setTaskSuccess(getTaskSuccess() + number);
			} else {
				logger.error("addSuccess exception");
			}
		}
	}
	
	public void addFail(){
		synchronized (TaskStatistics.class) {
			if(getTaskDoing() > 0 && (getTaskTotal() - 1) >= getTaskException()){
				this.setTaskException(getTaskException() + 1);
				setTaskDoing(getTaskDoing() - 1);
			} else {
				logger.error("addFail exception");
			}
		}
	}
	
	public void addFail(int number){
		synchronized (TaskStatistics.class) {
			if(getTaskDoing() > 0 && (getTaskTotal() - number) >= getTaskException()){
				this.setTaskException(getTaskException() + number);
			} else {
				logger.error("addFail exception");
			}
		}
	}
}
